import pygame, sys, math

class PlayerGun(pygame.sprite.Sprite):
    def __init__(self, pos = (0,0)):
        pygame.sprite.Sprite.__init__(self, self.containers)
        mousePos = pygame.mouse.get_pos()
        self.baseImage = pygame.image.load("rsc/guns/player/gunRight.png")
        self.rect = self.baseImage.get_rect()
        self.angle = ((math.atan2(mousePos[1] - self.rect.center[1], mousePos[0] - self.rect.center[0]))/math.pi)*180
        self.angle = -self.angle
        rot_image = pygame.transform.rotate(self.baseImage, self.angle)
        rot_rect = self.rect.copy()
        rot_rect.center = rot_image.get_rect().center
        rot_image = rot_image.subsurface(rot_rect)
        self.image = rot_image
        self.place(pos)
        
    
    def fixLocation(self, x, y):
        pass
    
    def place(self, pos):
        self.rect.center = pos
        
    def update(*args):
        self = args[0]
        playerSpeedX = args[2]
        playerSpeedY = args[3]
        playerScrollingX = args[4]
        playerScrollingY = args[5]
        playerx = args[8]
        playery = args[9]
        mousePos = pygame.mouse.get_pos()
        self.angle = ((math.atan2(mousePos[1] - self.rect.center[1], mousePos[0] - self.rect.center[0]))/math.pi)*180
        self.angle = -self.angle
        if mousePos[0] >= playerx:
            self.baseImage = pygame.image.load("rsc/guns/player/gunRight.png")
        else:
            self.baseImage = pygame.image.load("rsc/guns/player/gunLeft.png")
        rot_image = pygame.transform.rotate(self.baseImage, self.angle)
        rot_rect = self.rect.copy()
        rot_rect.center = rot_image.get_rect().center
        rot_image = rot_image.subsurface(rot_rect)
        self.image = rot_image
        if playerSpeedX > 0:
            if playerSpeedY == 0:
                self.rect.center = [playerx + playerSpeedX, playery - 5]
            elif playerSpeedY > 0:
                self.rect.center = [playerx + playerSpeedX, playery + playerSpeedY - 5]
            elif playerSpeedY < 0:
                self.rect.center = [playerx + playerSpeedX, playery + playerSpeedY - 5]
        elif playerSpeedX == 0:
            if playerSpeedY == 0:
                self.rect.center = [playerx, playery - 5]
            elif playerSpeedY > 0:
                self.rect.center = [playerx, playery + playerSpeedY - 5]
            elif playerSpeedY < 0:
                self.rect.center = [playerx, playery + playerSpeedY - 5]
        elif playerSpeedX < 0:
            if playerSpeedY == 0:
                self.rect.center = [playerx + playerSpeedX, playery - 5]
            elif playerSpeedY > 0:
                self.rect.center = [playerx + playerSpeedX, playery + playerSpeedY - 5]
            elif playerSpeedY < 0:
                self.rect.center = [playerx + playerSpeedX, playery + playerSpeedY - 5]